/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller.c;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.SearchTypeEnum;
import com.ykht.proj.coupon.service.CnBrandService;
import com.ykht.proj.coupon.service.CnShopService;
import com.ykht.proj.coupon.entity.CnShop;
import com.ykht.proj.coupon.entity.FxCmsInfo;
import com.ykht.proj.coupon.entity.FxWaiterExt;
import com.ykht.proj.coupon.service.FxCmsInfoService;
import com.ykht.proj.coupon.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


/**
 * cms资讯表
 *
 * @author proj code generator
 * @date 2021-02-01 16:22:52
 */
@RestController
@AllArgsConstructor
@RequestMapping("c/fxcmsinfo")
@Api(value = "fxcmsinfo", tags = "c端文章相关")
@Slf4j
public class CmsInfoController {

	private final FxCmsInfoService fxCmsInfoService;
	private final CnShopService cnShopService;
	private final CnBrandService cnBrandService;


	@ApiOperation(value = "推广精彩内容列表")
	@GetMapping("/pushCmsPage")
	public R<Page<PushCms4C>> pushCmsPage(Page page,BaseLonLat lonLat) {
		log.info("--------------->推广精彩内容列表：{},{}",page,lonLat);
		page =  fxCmsInfoService.pushCmsPage(page,lonLat);
		return R.ok(page);
	}
	@ApiOperation(value = "滑动视频")
	@GetMapping("/slideVideo")
	public R slideVideo() {
		log.info("--------------->滑动视频：{},{},{}");
		return null;
	}
	@ApiOperation(value = "首页资讯")
	@GetMapping("/home")
	public R<Page<CmsTemplateCover>> home(Page page,CmsTemplateCoverReq req) {
		log.info("--------------->首页资讯：{},{}",page,req);
//		req.setDistance(10000L);
		return fxCmsInfoService.home(page,req);
	}

	/**
	 * （分享进来的文章需要传waiterId获取门店电话）
	 * @param id
	 * @param addCouponFlag
	 * @param waiterId
	 * @param lonLat
	 * @return
	 */
	@ApiOperation(value = "c端文章详情", notes = "文章详情")
	@GetMapping("/{id}")
	public R<CmsInfoTemplate> getById(@PathVariable("id") Long id, Integer addCouponFlag, Long waiterId, BaseLonLat lonLat) {
		log.info("--------------->c端文章详情：{},{},{}", id, addCouponFlag, waiterId,lonLat);
		return fxCmsInfoService.getCmsInfoTemplateById(id, addCouponFlag, waiterId,lonLat);
	}

	/**
	 * 品牌时讯
	 */
	@ApiOperation(value = "品牌时讯", notes = "品牌时讯")
	@GetMapping("/brandInformation")
	public R getBrandInformation(Page page, FxCmsInfoVO fxCmsInfoVO) {
		IPage<FxCmsInfoVO> iPage = fxCmsInfoService.getBrandInformation(page, fxCmsInfoVO);
		return R.ok(iPage);
	}

	/**
	 * 门店时讯
	 */
	@ApiOperation(value = "门店时讯", notes = "门店时讯")
	@GetMapping("/shopInformation")
	public R getShopInformation(Page page, FxCmsInfoVO fxCmsInfoVO) {
		IPage<FxCmsInfoVO> iPage = fxCmsInfoService.getBrandInformation(page, fxCmsInfoVO);
		return R.ok(iPage);
	}



	/**
	 * 根据品牌ID查询 品牌时讯
	 */
	@ApiOperation(value = "根据品牌ID查询 品牌时讯", notes = "根据品牌ID查询 品牌时讯")
	@GetMapping("/getBrandCmsPage")
	public R getBrandCmsPage(Page page, FxCmsInfoVO fxCmsInfoVO) {
		log.info("---------->根据品牌ID查询 品牌时讯：{},{}", page, fxCmsInfoVO);
		if (null == fxCmsInfoVO.getBrandId()){
			CnShop cnShop = new CnShop().selectById(fxCmsInfoVO.getShopId());
			fxCmsInfoVO.setBrandId(cnShop.getBrandId());
		}
		IPage<FxCmsInfoVO> iPage = fxCmsInfoService.getBrandCmsPage(page, fxCmsInfoVO);
		return R.ok(iPage);
	}

	/**
	 * 根据品牌切换视频
	 */
	@ApiOperation(value = "根据品牌切换视频", notes = "根据品牌切换视频")
	@GetMapping("/getBrandSwitchVideoPage")
	public R getBrandSwitchVideoPage(Page page, FxCmsInfoVO fxCmsInfoVO) {
		log.info("---------->根据品牌切换视频:{},{}", page, fxCmsInfoVO);
		if (null == fxCmsInfoVO.getBrandId()){
			CnShop cnShop = new CnShop().selectById(fxCmsInfoVO.getShopId());
			fxCmsInfoVO.setBrandId(cnShop.getBrandId());
		}
		page.setSize(9999L);
		IPage<FxCmsInfoVO> iPage = fxCmsInfoService.getBrandSwitchVideoPage(page, fxCmsInfoVO);
		return R.ok(iPage.getRecords());
	}

	/**
	 * 根据首页切换视频
	 */
	@ApiOperation(value = "根据首页切换视频", notes = "根据首页切换视频")
	@GetMapping("/getHomeSwitchVideoPage")
	public R getHomeSwitchVideoPage(Page page, FxCmsInfoVO fxCmsInfoVO) {
		log.info("---------->根据首页切换视频:{},{}", page, fxCmsInfoVO);
		page.setSize(9999L);
		IPage<FxCmsInfoVO> iPage = fxCmsInfoService.getHomeSwitchVideoPage(page, fxCmsInfoVO);
		return R.ok(iPage.getRecords());
	}


	/**
	 * 精彩推荐
	 */
	@ApiOperation(value = "精彩推荐", notes = "精彩推荐")
	@GetMapping("/wonderfulRecommend")
	public R getWonderfulRecommend(Page page, FxCmsInfoVO fxCmsInfoVO) {
		IPage<FxCmsInfoVO> iPage = fxCmsInfoService.getWonderfulRecommend(page, fxCmsInfoVO);
		return R.ok(iPage);
	}

	/**
	 * 添加阅读量
	 */
	@ApiOperation(value = "添加阅读量", notes = "添加阅读量")
	@PutMapping("/addReadCount")
	public R setReadCount(@RequestBody FxCmsInfo fxCmsInfo) {
		return R.ok(fxCmsInfoService.addReadCount(fxCmsInfo));
	}

	/*
	 * 任务列表
	 * */
	@ApiOperation(value = "每日任务列表", notes = "每日任务列表")
	@GetMapping("/taskList")
	public R getTaskList() {
		FxWaiterExt waiterExt = new FxWaiterExt().selectOne(Wrappers.lambdaQuery(FxWaiterExt.class)
				.eq(FxWaiterExt::getLoginId, SecurityUtils.getUserId()).eq(FxWaiterExt::getWorkState, 1));
		/*根据品牌id去获取对应的文章*/
		return R.ok(fxCmsInfoService.queryByBrandId(waiterExt));
	}


	/**
	 * S-01首页（文章搜索（精选内容））分页查询
	 */
	@ApiOperation(value = "S-01首页（文章搜索（精选内容））分页查询", notes = "S-01首页（文章搜索（精选内容））分页查询")
	@GetMapping("/getCmsRecommendPage")
	public R getCmsRecommendPage(Page page, HomePageSearch homePageSearch) {
		log.info("---->S-01首页（文章搜索（精选内容））分页查询:{},{}", page, homePageSearch);
		HashMap<String, Object> hashMap = new HashMap<>();
		switch (SearchTypeEnum.getThisByVal(homePageSearch.getSearchType())) {
			case whole:
				IPage<FxCmsInfoVO> cmsPage = fxCmsInfoService.getCmsRecommendPage(BeanUtil.toBean(page, Page.class), homePageSearch);
				hashMap.put("cmsPage", cmsPage);
				IPage<CnShopVo> shopPage = cnShopService.getCmsShopPage(BeanUtil.toBean(page, Page.class), homePageSearch);
				hashMap.put("shopPage", shopPage);
				IPage<CnBrandVo> brandPage = cnBrandService.getCmsBrandPage(BeanUtil.toBean(page, Page.class), homePageSearch);
				hashMap.put("brandPage", brandPage);
				break;
			case brand:
				IPage<CnBrandVo> brandPage1 = cnBrandService.getCmsBrandPage(page, homePageSearch);
				hashMap.put("brandPage", brandPage1);
				break;
			case shop:
				IPage<CnShopVo> shopPage1 = cnShopService.getCmsShopPage(page, homePageSearch);
				hashMap.put("shopPage", shopPage1);
				break;
			case cms:
				IPage<FxCmsInfoVO> cmsPage1 = fxCmsInfoService.getCmsRecommendPage(page, homePageSearch);
				hashMap.put("cmsPage", cmsPage1);
				break;
		}
		return R.ok(hashMap);
	}
}
